/*
 * Copyright 2011 www.pretty-tools.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "stdafx.h"
#include "SimpleEnumerator.h"


// CSimpleEnumerator
CSimpleEnumerator::CSimpleEnumerator()
    : m_index(0)
{
    ATLTRACE( _T("CSimpleEnumerator::CSimpleEnumerator()\n") );
}

CSimpleEnumerator::~CSimpleEnumerator()
{
    ATLTRACE( _T("CSimpleEnumerator::~CSimpleEnumerator()\n") );
}

STDMETHODIMP CSimpleEnumerator::Init(VARIANT values)
{
    m_values.Destroy();
    m_index = 0;

    if ((VT_ARRAY|VT_VARIANT) != values.vt)
        return E_INVALIDARG;

    m_values = values.parray;

    ATLTRACE( _T("CSimpleEnumerator::Init - %i elements\n"), m_values.GetCount() );

    return S_OK;
}

STDMETHODIMP CSimpleEnumerator::hasMoreElements(VARIANT_BOOL* pRet)
{
    if (!pRet)
        return E_POINTER;

    *pRet = ToVariant(m_index < m_values.GetCount());

    return S_OK;
}

STDMETHODIMP CSimpleEnumerator::getNext(VARIANT* pRet)
{
    if (!pRet)
        return E_POINTER;

    return ::VariantCopy(pRet, &m_values[m_index++]);
}
